package com.example.hans.navigationbyviewpageandradiobutton;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

public class MsgAdapterForRecyclerView extends RecyclerView.Adapter<MsgAdapterForRecyclerView.ViewHolder> {

    private List<Msg> mMsgList;

    public class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout leftLinearLayout;
        LinearLayout rightLinearLayout;
        TextView leftMsg;
        TextView rightMsg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            leftLinearLayout = itemView.findViewById(R.id.left_layout);
            rightLinearLayout = itemView.findViewById(R.id.right_layout);
            leftMsg = itemView.findViewById(R.id.tv_left_msg);
            rightMsg = itemView.findViewById(R.id.tv_right_msg);
        }
    }

    public MsgAdapterForRecyclerView(List<Msg> mMsgList) {
        this.mMsgList = mMsgList;
    }




    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()
        ).inflate(R.layout.msg_item,viewGroup,false);
        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        Msg msg = mMsgList.get(position);
        if (msg.getType() == Msg.TYPE_RECEIVED) {
            viewHolder.leftLinearLayout.setVisibility(View.VISIBLE);
            viewHolder.rightLinearLayout.setVisibility(View.GONE);
            viewHolder.leftMsg.setText(msg.getContent());
        } else if (msg.getType() == Msg.TYPE_SEND){
            viewHolder.leftLinearLayout.setVisibility(View.GONE);
            viewHolder.rightLinearLayout.setVisibility(View.VISIBLE);
            viewHolder.rightMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}
